package com.iss.okair.db.mapper.sql;

import com.iss.common.StringTools;
import com.iss.okair.db.entity.AssetsNormal;
import com.iss.okair.db.model.AssetsNormalMode;
import org.apache.ibatis.jdbc.SQL;

public class AssetsNormalSQL {
    public String find (AssetsNormalMode model){
        return new SQL(){{
            SELECT("*");
            FROM("assets_normal");
            if(model.getAssetsNameCn()!=null && !model.getAssetsNameCn().isEmpty()){
                WHERE("namecn like '%"+model.getAssetsNameCn()+"%'");
            }
            if(model.getAssetsCode()!=null && !model.getAssetsCode().isEmpty()){
                WHERE("assetscode like'%"+model.getAssetsCode()+"%'");
            }
            if(model.getSupplier()!= null && !model.getSupplier().isEmpty()){
                WHERE("suppliper like '%"+model.getSupplier()+"%'");
            }
            if(model.getPackageType()!=null && !"-1".equals(model.getPackageType())){
                WHERE("packageType="+model.getPackageType());
            }
            if(model.isConsumptive()){
                WHERE("consumptive=1");
            }
            //无意义操作
//            else{
//                WHERE("consumptive=0");
//            }
            if(model.getAssetsType2()!=null && !"-1".equals(model.getAssetsType2())){
                WHERE("assetsType="+model.getAssetsType2());
            }
            if (model.getAssetsType1()!= null && !"-1".equals(model.getAssetsType1()) && "-1".equals(model.getAssetsType2()))
            {
                WHERE("assetsType in (select id from category where parentId="+model.getAssetsType1()+")");
            }

        }}.toString();




    }

    //生成修改的动态SQL语句的方法
    public String update(AssetsNormal normal){
        return new SQL (){{
            UPDATE("assets_normal");
            if(normal.getAssetstype() != null && normal.getAssetstype() != -1){
                SET("assetstype=#{assetstype}");
            }

            if(normal.getPackagetype() != null && normal.getPackagetype() != -1){
                SET("packagetype=#{packagetype}");
            }
            if(!StringTools.isNullOrEmpty(normal.getNamecn())){
                SET("namecn=#{namecn}");
            }
            if(!StringTools.isNullOrEmpty(normal.getSupplier())){
                SET("supplier=#{supplier}");
            }
            if(!StringTools.isNullOrEmpty(normal.getNormalbrand())){
                SET("normalbrand=#{normalbrand}");
            }

            if(!StringTools.isNullOrEmpty(normal.getNormalmodel())){
                SET("normalmodel=#{normalmodel}");
            }
            if(!StringTools.isNullOrEmpty(normal.getNormalmodel())){
                SET("normalmodel=#{normalmodel}");
            }

            if(!StringTools.isNullOrEmpty(normal.getSuppliertel())){
                SET("suppliertel=#{suppliertel}");
            }

            if(!StringTools.isNullOrEmpty(normal.getComment())){
                SET("comment=#{comment}");
            }

            if(normal.getConsumptive()!=null){
                SET("consumptive=#{consumptive}");
            }
            WHERE("id=#{id}");

        }}.toString();
    }

}


